<template>
  <el-form
    ref="ruleFormRef"
    :model="csForm"
    :rules="rules"
    class="demo-ruleForm"
    label-position="top"
    size="small"
  >
    <el-form-item
      :label="t('plc.acknowledgementRequired')"
      prop="acknowledgementRequired"
    >
      <el-switch v-model="csForm.acknowledgementRequired" />
    </el-form-item>
    <el-form-item
      :label="t('plc.com')"
      prop="payload.com"
      placeholder="组件编号"
    >
      <el-input
        v-model="csForm.payload.com"
        @keyup.enter="submitForm(ruleFormRef)"
      />
    </el-form-item>

    <div
      v-for="(item, index) in csForm.payload.cls"
      :key="index"
    >
      <el-icon style="float: right">
        <Plus
          v-if="index === 0"
          @click="addCls"
        />
        <Minus
          v-else
          @click="removeCls(index)"
        />
      </el-icon>
      <el-form-item
        :label="t('plc.car')"
        prop="payload.cls[0].car"
        placeholder="载体编号"
      >
        <el-input
          v-model="csForm.payload.cls[index].car"
          @keyup.enter="submitForm(ruleFormRef)"
        />
      </el-form-item>
      <el-form-item
        :label="t('plc.wgt')"
        prop="payload.cls[0].wgt"
        placeholder="重量"
      >
        <el-input-number
          :min="0"
          v-model="csForm.payload.cls[index].wgt"
        />
      </el-form-item>
      <el-form-item
        :label="t('plc.len')"
        prop="payload.cls[0].len"
        placeholder="长度"
      >
        <el-input-number
          :min="0"
          v-model="csForm.payload.cls[index].len"
        />
      </el-form-item>
      <el-form-item
        :label="t('plc.wdt')"
        prop="payload.cls[0].wdt"
        placeholder="宽度"
      >
        <el-input-number
          :min="0"
          v-model="csForm.payload.cls[index].wdt"
        />
      </el-form-item>
      <el-form-item
        :label="t('plc.hgt')"
        prop="payload.cls[0].hgt"
        placeholder="高度"
      >
        <el-input-number
          :min="0"
          v-model="csForm.payload.cls[index].hgt"
        />
      </el-form-item>
      <el-form-item
        :label="t('plc.dex')"
        prop="payload.cls[0].dex"
        placeholder="包裹标记"
      >
        <el-input-number
          :min="0"
          v-model="csForm.payload.cls[index].dex"
        />
      </el-form-item>
      <el-form-item
        :label="t('plc.her')"
        prop="wgt"
        placeholder="高度是否异常"
      >
        <el-switch v-model="csForm.payload.cls[index].her" />
      </el-form-item>
    </div>

    <el-form-item>
      <el-button
        type="primary"
        @click="submitForm(ruleFormRef)"
        :disabled="!plcServerStatus"
        :loading="loading"
      >
        {{ t('plc.send') }}
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">
        {{ t('plc.reset') }}
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useI18n } from 'vue-i18n'
import { sendMessageByServer } from '@/api/plc'
import { plcStore } from '@/store/plcStore'
import { storeToRefs } from 'pinia'

const store = plcStore()
const loading = ref(false)
const { plcServerStatus, plcServerPort } = storeToRefs(store) // 连接状态
const { t } = useI18n()
const ruleFormRef = ref<FormInstance>()

const props = defineProps({
  ip: {
    type: String,
    required: false,
    default: '127.0.0.1',
  },
  port: {
    type: Number,
    required: false,
    default: 2000,
  },
})

type CarrierBlock = {
  car: string
  wgt: number
  her: boolean
  len: number
  wdt: number
  hgt: number
}

const csForm = reactive({
  acknowledgementRequired: false,
  payloadType: 'CONV',
  payload: {
    com: '',
    evt: 'CS',
    cls: [
      {
        car: '',
        wgt: 0,
        her: false,
        len: 0,
        wdt: 0,
        hgt: 0,
        dex: 0
      },
    ],
  },
})

const rules = reactive<FormRules>({
  'payload.cls[0].car': [
    {
      required: true,
      message: '请填写载体编号',
      trigger: 'blur',
    },
  ],
  'payload.com': [
    {
      required: true,
      message: '请填写组件编号',
      trigger: 'blur',
    },
  ],
  'payload.cls[0].wgt': [{ type: 'number', message: '方向码必须为数字类型' }],
  'payload.cls[0].her': [],
  'payload.cls[0].len': [{ type: 'number', message: '长度必须为数字类型' }],
  'payload.cls[0].wdt': [{ type: 'number', message: '宽度必须为数字类型' }],
  'payload.cls[0].hgt': [{ type: 'number', message: '高度必须为数字类型' }],
  'payload.cls[0].dex': [{ type: 'number', message: '包裹标记必须为数字类型' }],
})

// onMounted(() => {
//   csForm.payload.cls.push(defaultCarrierBlock)
// })

/**
 * 添加载体
 */
const addCls = () => {
  csForm.payload.cls.push({
    car: '',
    wgt: 0,
    her: false,
    len: 0,
    wdt: 0,
    hgt: 0,
    dex: 0
  })
}

/**
 * 减少载体
 */
const removeCls = (id) => {
  csForm.payload.cls.splice(id, 1)
}

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true
      sendMessageByServer(csForm)
        .then((res) => {
          ElMessage({
            showClose: true,
            message: res.data.message,
            type: 'success',
            duration: 1000,
          })
        })
        .finally(() => {
          loading.value = false
        })
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
